
import java.awt.Component;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author lupo
 */
public class NewTournament extends javax.swing.JFrame {

    MainMenu mainMenu;
    
    /**
     * Creates new form NewTournament
     */
    public NewTournament(MainMenu mainMenu) {
        this.mainMenu = mainMenu;
        initComponents();
        
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        goLazadaButton = new javax.swing.JButton();
        tournamentNameLabel = new javax.swing.JLabel();
        tournamentNameTField = new javax.swing.JTextField();
        capadaLabel = new javax.swing.JLabel();
        lazosLabel = new javax.swing.JLabel();
        capadaSpinner = new javax.swing.JSpinner();
        lazosSpinner = new javax.swing.JSpinner();
        acceptButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        goLazadaButton.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        goLazadaButton.setText("Ir");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Lazada Sierra Grande - Lienzo");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        tournamentNameLabel.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        tournamentNameLabel.setText("Nombre del Torneo:");

        tournamentNameTField.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        tournamentNameTField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tournamentNameTFieldKeyTyped(evt);
            }
        });

        capadaLabel.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        capadaLabel.setText("Capada:");

        lazosLabel.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        lazosLabel.setText("Lazos:");

        capadaSpinner.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        capadaSpinner.setModel(new javax.swing.SpinnerNumberModel(8, 6, 18, 1));

        lazosSpinner.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        lazosSpinner.setModel(new javax.swing.SpinnerNumberModel(8, 6, 12, 1));

        acceptButton.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        acceptButton.setText("Aceptar");
        acceptButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                acceptButtonActionPerformed(evt);
            }
        });

        cancelButton.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        cancelButton.setText("Cancelar");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(tournamentNameTField))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(tournamentNameLabel)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(68, 68, 68)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(acceptButton, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(15, 15, 15)
                                .addComponent(capadaLabel))
                            .addComponent(capadaSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 64, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(lazosLabel)
                                .addGap(57, 57, 57))
                            .addComponent(lazosSpinner, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(81, 81, 81))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(tournamentNameLabel)
                .addGap(18, 18, 18)
                .addComponent(tournamentNameTField, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(capadaLabel)
                    .addComponent(lazosLabel))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(capadaSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lazosSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(acceptButton, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(41, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-503)/2, (screenSize.height-391)/2, 503, 391);
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        //dispose();
        mainMenu.setVisible(true);
    }//GEN-LAST:event_formWindowClosed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        dispose();
        mainMenu.setVisible(true);
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void acceptButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_acceptButtonActionPerformed
        //Check if a name for the tournament has been entered
        if (tournamentNameTField.getText().contentEquals("")){
            Component frame = null;
            JOptionPane.showMessageDialog(frame,
            "Por favor proporcione un nombre para el torneo","Lazada Sierra Grande - Lienzo",
            JOptionPane.PLAIN_MESSAGE);
        }
        else{
            //Erase db
            List<Contestant> contestants = mainMenu.db.query(Contestant.class);
            for (int i=0; i<contestants.size(); i++)
                mainMenu.db.delete(contestants.get(i));
            
            List<Pair> pairs = mainMenu.db.query(Pair.class);
            for (int i=0; i<pairs.size(); i++)
                mainMenu.db.delete(pairs.get(i));
            
            List<Tournament> tournaments = mainMenu.db.query(Tournament.class);
            for (int i=0; i<tournaments.size(); i++)
                mainMenu.db.delete(tournaments.get(i));
            
            short lazos = (short)((int)lazosSpinner.getValue());
            mainMenu.lazos = lazos;
            dispose();
            mainMenu.setVisible(true);
            mainMenu.createNewTournament(tournamentNameTField.getText(),
                    (short)((int)capadaSpinner.getValue()),
                    (short)((int)lazosSpinner.getValue()));
            mainMenu.canShuffle = true;
            mainMenu.capada = (short)((int)capadaSpinner.getValue());
            mainMenu.lazadaSpinner.removeAllItems();
            for (int i=0; i< lazos; i++)
                mainMenu.lazadaSpinner.addItem("Lazo #"+(i+1));
            mainMenu.isOpen = true;
            mainMenu.openRegistry();
            
            mainMenu.db.commit();
        }
    }//GEN-LAST:event_acceptButtonActionPerformed

    private void tournamentNameTFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tournamentNameTFieldKeyTyped
        final String previousText = tournamentNameTField.getText();
        if (tournamentNameTField.getText().length()>=24){
            java.awt.EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    tournamentNameTField.setText(previousText);
                }
            });
        }
    }//GEN-LAST:event_tournamentNameTFieldKeyTyped

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton acceptButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel capadaLabel;
    private javax.swing.JSpinner capadaSpinner;
    private javax.swing.JButton goLazadaButton;
    private javax.swing.JLabel lazosLabel;
    private javax.swing.JSpinner lazosSpinner;
    private javax.swing.JLabel tournamentNameLabel;
    private javax.swing.JTextField tournamentNameTField;
    // End of variables declaration//GEN-END:variables
}
